/**
 * 
 */
package es.cloudcast.server.persistence.jdo.action.basic;

import javax.jdo.PersistenceManager;

import es.cloudcast.server.persistence.PersistenceException;
import es.cloudcast.server.persistence.dao.jdo.PersistenceManagerFactory;
import es.cloudcast.server.persistence.jdo.action.Action;

/**
 * Saves the object in the underlying datastore
 * 
 * @author Samuel García Martínez <in0de>
 *
 */
public class SaveAction<T> implements Action<T> {

	protected T transientInstance;
	
	public SaveAction(T transientInstance) {
		this.transientInstance = transientInstance;
	}
	
	/* (non-Javadoc)
	 * @see es.cloudcast.persistence.jdo.action.Action#process(es.cloudcast.persistence.jdo.PersistenceManagerFactory)
	 */
	public T process(PersistenceManagerFactory PMF)
			throws PersistenceException {
		PersistenceManager pManager = PMF.getCurrentPersistenceManager();
		
		try {
			pManager.makePersistent(transientInstance);
		} catch(Exception ex) {
			throw new PersistenceException(ex);
		}
		
		return transientInstance;
	}

}
